<template>
  <div class="page">
    <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
      <el-form-item label="事项">
        <el-select v-model="formInline.matter" placeholder="事项">
          <el-option v-for="item in matterList" :key="item.id" :label="item.item_name" :value="item.item_name" />
        </el-select>
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker
          v-model="formInline.time"
          type="daterange"
          format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-search" size="mini" @click="onSubmit">搜索</el-button>
      </el-form-item>
      <el-form-item style="float: right">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addModelClick">新增</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{background:'#F2F2F2',color:'#333333'}"
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          type="index"
          width="60"
        />
        <el-table-column
          prop="name"
          label="姓名"
          width="100"
        />
        <el-table-column label="申请事项" prop="item_name" />
        <el-table-column label="申请时间" prop="apply_time" width="100" />
        <el-table-column label="地点" prop="address" />
        <el-table-column label="预约时间" prop="time" />
        <el-table-column label="状态" prop="status" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.status == '待审核'" :style="yellow">{{ scope.row.status }}</span>
            <span v-else-if="scope.row.status == '待派发'" :style="orange">{{ scope.row.status }}</span>
            <span v-else-if="scope.row.status == '已派发'" :style="green">{{ scope.row.status }}</span>
            <span v-else-if="scope.row.status == '待上门'" :style="orange">{{ scope.row.status }}</span>
            <span v-else-if="scope.row.status == '待办理'" :style="orange">{{ scope.row.status }}</span>
            <span v-else-if="scope.row.status == '已驳回'" :style="red">{{ scope.row.status }}</span>
            <span v-else :style="green">{{ scope.row.status }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" align="right">
          <template slot-scope="scope">
            <!--<div v-if="scope.row.status == '待办理'" class="info_button" @click="examineModalClick(scope.row.id)">审核</div>-->
            <div v-if="scope.row.status == '待审核'" class="info_button" @click="examineModalClick(scope.row.id)">审核</div>
            <div v-if="scope.row.status == '待派发'" class="info_button" @click="handModalClick(scope.row.id)">派发工作人员</div>
            <div v-if="scope.row.status == '已派发'">
              <span class="sell_name">{{ scope.row.department }}</span>
              <span class="info_button" @click="replaceModalClick(scope.row.id,scope.row.department)">更换人员</span>
            </div>
            <div v-if="scope.row.status == '已驳回' || scope.row.status == '归档' || scope.row.status == '待上门' || scope.row.status == '待办理' || scope.row.status == '已归档'">
              <span class="sell_name">{{ scope.row.department }}</span>
              <span  class="info_button" @click="seeModalClick(scope.row.id)">查看</span>
              <span class="info_button" @click="replaceModalClick(scope.row.id,scope.row.department)" v-if="scope.row.status == '已驳回'&&scope.row.department != null">更换人员</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :page-size.sync="page.pageSize"
      :total="page.total"
      :current-page.sync="page.pageIndex"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!--审核弹框-->
    <examine-modal :apply-id="applyId" :examineModalShow="examineModalShow" @examineFun="getExamineData" />
    <!--待派发弹框-->
    <handle-modal :handle-id="handleId" :handModalShow="handModalShow" @handleFun="gethandleData" />
    <!--更换人员弹框-->
    <replacement-modal :apply-id="applyId" :department="department" :replaceModalShow="replaceModalShow" @replaceFun="getReplaceData" />
    <!--录入弹框-->
    <add-modal @addFun="getAddData" :addModelShow="addModelShow" />
    <!--查看弹框-->
    <detail-modal :apply-id="applyId" :seeModalShow="seeModalShow" @seeFun="getSeeData" />
  </div>
</template>

<script>
  // 审核弹框
  import ExamineModal from '../components/ExamineModal'
  // 待派发弹框
  import HandleModal from '../components/HandleModal'
  // 更换人员
  import ReplacementModal from '../components/ReplacementModal'
  // 录入弹框
  import AddModal from '../components/AddModal'
  // 查看弹框
  import DetailModal from '../components/DetailModal'

  import { getMatterDataList, getDispatchList } from '../api'
  import { dateFormat } from '@/views/assistance/utils'

  export default {
    name: 'Assistance',
    components: {
      ExamineModal,
      HandleModal,
      ReplacementModal,
      AddModal,
      DetailModal
    },
    data() {
      return {
        red: 'color: #FF2929',
        orange: 'color: #2AC23D',
        green: 'color: #542EFF',
        yellow: 'color:#FF8504',
        blue: '#1182FB',
        loading: false,
        formInline: {
          matter: '',
          time: '',
          start_time: '',
          end_time: ''
        },
        matterList: [{
          id: '',
          item_name: '全部'
        }],
        tableData: [
          // {
          //   item_name:'1',
          //   apply_time:'1',
          //   address:'1',
          //   item_name:'1',
          // }
        ],
        page: {
          pageSize: 12,
          pageIndex: 1,
          total: 0
        },
        applyId: '',
        handleId: '',
        department: '',
        addModelShow:false,
        examineModalShow:false,
        seeModalShow:false,
        handModalShow:false,
        replaceModalShow:false
      }
    },
    computed: {

    },
    watch: {
      'formInline.time': function(newVal) {
        this.formInline.start_time =
          newVal != null
            ? this.formatTime(newVal[0])
            : ''
        this.formInline.end_time =
          newVal != null
            ? this.formatTime(newVal[1])
            : ''
      }
    },
    mounted() {
      this.getMatterList()
      this.getDataList()
    },
    methods: {
      // 时间格式化
      formatTime(time) {
        return time ? dateFormat(new Date(time), 'yyyy-MM-dd') : ''
      },
      // 获取事项
      getMatterList() {
        getMatterDataList().then(res => {
          res.data.data.map((item) => {
            this.matterList.push(item)
          })
        })
      },
      // 获取列表信息
      getDataList() {
        const data = {
          ryid: (JSON.parse(sessionStorage.getItem('userInfo'))).id,
          item_name: this.formInline.matter,
          start_time: this.formInline.start_time,
          end_time: this.formInline.end_time,
          page: this.page.pageIndex,
          limit: this.page.pageSize
        }
        this.loading = true
        getDispatchList(data).then(res => {
          this.loading = false
          this.tableData = res.data.data.list
          this.page.total = res.data.data.totalRow
          this.tableData.map((item, index) => {
            this.tableData[index].apply_time = this.formatTime(item.apply_time)
          })
        })
      },
      onSubmit() {
        this.getDataList()
      },
      handleSizeChange(val) {
        this.page.pageSize = val
        this.getDataList()
        console.log(`每页 ${val} 条`)
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.page.pageIndex = val
        this.getDataList()
      },
      // 审核
      examineModalClick(id) {
        this.applyId = id;
        this.examineModalShow= true;
      },
      handModalClick(id) {
        this.handleId = id;
        this.handModalShow = true;
      },
      replaceModalClick(id, department) {
        this.applyId = id;
        this.replaceModalShow = true;
        this.department = department;
      },
      getReplaceData(data) {
        if(data){
          this.getDataList()
        }
        this.replaceModalShow = false;
      },
      getAddData(data) {
        if(data){
          this.getDataList()
        }
        this.addModelShow = false;
      },
      getSeeData(){
        this.seeModalShow = false;
      },
      getExamineData(data) {
        if(data){
          this.getDataList()
        }
        this.examineModalShow = false;

      },
      gethandleData(data) {
        if(data){
          this.getDataList()
        }
        this.handModalShow = false;
      },
      addModelClick() {
        this.addModelShow = true;
      },
      seeModalClick(id) {
        this.applyId = id;
        this.seeModalShow = true;
      }
    }
  }
</script>

<style lang="scss" scoped>
  .page{
    position: relative;
    height: calc(100vh - 110px);
    padding: 20px 20px 45px 20px;
    .info_button {
      color: #4885F4;
      cursor: pointer;
    }
    .sell_name{
      padding-right: 10px;
    }
  }

</style>
